/**
 * @desc user system app js, the main entry
 * @author wangXi
 * @date 2015/12/2
 */

requirejs.config({
    baseUrl: 'js/lib/',
    //debug模式 不缓存js
    urlArgs: "_t=" +  (new Date()).getTime(),
    paths: {
        jquery: 'jquery-1.8.3.min',
        app_conf: '../app_config'
    }
});


require(['jquery', 'wxi', 'app_conf'], function($, w, conf){
    var valid_passwd=function(pwd){
        return /^\w{6,20}$/.test(pwd);
    };

    $(function(){
        var login_wrap = $('.fm-login-wrap'),
            ele_uname=$('#username'),
            ele_pwd=$('#password');

        var handle_submit=function(post_data){
            $.post(conf.req_url, post_data, function(resp){
                w.util.tipMsg('',resp.info, function(){
                    if(resp.success==true){
                        login_wrap.remove();

                        //登录成功，页面跳转
                        var redirect_url='';
                        var hf=location.href.match(/redirect=(.+)/);
                        if(hf && hf[1]){
                            redirect_url=hf[1];
                        }
                        else{
                            redirect_url=conf.default_redirect
                        }
                        location.href=redirect_url;

                    }
                    else{
                        if(resp.code=='0103'){
                            //token失效，重刷页面
                            location.reload(true);
                        }

                        $(ele_uname).add(ele_pwd).val('');
                        ele_uname.focus();

                    }
                });

            },'json');
        };

        //提交登录表单
        $('#btn_login').on('click', function(){
            var user_name = ele_uname.val(),
                user_pass = ele_pwd.val();

            if(user_name == '' || user_pass == ''){
                w.util.tipMsg('用户名和密码不能为空');
            }
            else if( !(w.string.isPhone(user_name) || w.string.isEmail(user_name)) ){
                w.util.tipMsg('用户名为手机号或者邮箱');
            }
            else if( !valid_passwd(user_pass) ){
                w.util.tipMsg( '密码格式错误,长度为6 ~ 20位');
            }
            else{
                //发起login请求
                var post_data = w.string.qs2obj($(this).parents('form').serialize());
                post_data['act']='login';
                handle_submit(post_data);
            }

        });


        //提交注册表单
        $('#btn_register').on('click', function(){
            var user_name = ele_uname.val(),
                user_pass = ele_pwd.val();

            if(user_name == '' || user_pass == ''){
                w.util.tipMsg( '用户名和密码不能为空');
            }
            else if( !(w.string.isPhone(user_name) || w.string.isEmail(user_name)) ){
                w.util.tipMsg( '请填写正确的手机号或邮箱');
            }
            else if( !valid_passwd(user_pass) ){
                w.util.tipMsg( '密码格式错误');
            }
            else{
                //发起register请求
                var post_data = w.string.qs2obj($(this).parents('form').serialize());
                post_data['act']='register';
                handle_submit(post_data);
            }

        });

        ele_uname.focus();
        $('input.wx-login-ctrl').on('focus keydown', function(e){
            if(e.which == 13){
                login_wrap.find('.btn_group > input:first-child').trigger('click');
            }
        });

    });


});